//package com.fecean.aabbc.netty.server.prod4;
//
//import com.fecean.aabbc.netty.message4.MessageNotice;
//import com.fecean.aabbc.netty.message4.MessageRequest;
//import com.fecean.aabbc.netty.message4.MessageResponse;
//import com.fecean.aabbc.netty.message4.ResponseFuture;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.time.Instant;
//import java.util.Optional;
//import java.util.UUID;
//
///**
// * NettyClientSender
// *
// * @author: chenfenghai
// * @version: 1.0
// * @date: 2020-03-12 16:44
// * @blame Team
// */
//@Slf4j
//@Component
//public class NettyClientSender {
//
//    @Resource
//    NettyClientGroup nettyClientGroup;
//
//    /**
//     ** 发送通知
//     * @param tenantId 租户ID
//     * @param cinemaCode 影院编码
//     * @param content 消息内容
//     */
//    public void sendNotice(String tenantId, String cinemaCode, String content){
//        Optional<NettyClientWrapper> optionalChannel = nettyClientGroup.getClientChannel(tenantId, cinemaCode);
//        optionalChannel.ifPresent(client -> {
//            MessageNotice notice = new MessageNotice();
//            notice.setId(UUID.randomUUID().toString());
//            notice.setTimestamp(Instant.now().toEpochMilli());
//            notice.setTenantId(tenantId);
//            notice.setCinemaCode(cinemaCode);
//            notice.setContent(content);
//            client.getChannel().writeAndFlush(notice);
//            log.info("客户端[{}-{}]发送通知：{}",
//                    notice.getTenantId(), notice.getCinemaCode(), notice);
//        });
//    }
//
//    /**
//     ** 发送请求
//     * @param tenantId 租户ID
//     * @param cinemaCode 影院编码
//     * @param content 消息内容
//     */
//    public MessageResponse sendRequest(String tenantId, String cinemaCode, String content){
//        Optional<NettyClientWrapper> optionalChannel = nettyClientGroup.getClientChannel(tenantId, cinemaCode);
//        if(optionalChannel.isPresent()){
//            MessageRequest request = new MessageRequest();
//            request.setId(UUID.randomUUID().toString());
//            request.setTimestamp(Instant.now().toEpochMilli());
//            request.setTenantId(tenantId);
//            request.setCinemaCode(cinemaCode);
//            request.setContent(content);
//            ResponseFuture future = new ResponseFuture(optionalChannel.get().getChannel(), request);
//            optionalChannel.get().getChannel().writeAndFlush(request);
//            log.info("客户端[{}-{}]发送请求：{}",
//                    request.getTenantId(), request.getCinemaCode(), request);
//            // 等待获取响应结果
//            return future.get();
//        }
//        return null;
//    }
//}
